package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.mapper.BankAccMapper;
import com.jzo2o.customer.model.domain.BankAcc;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IBankAccService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-02
 */
@Service
public class BankAccServiceImpl extends ServiceImpl<BankAccMapper, BankAcc> implements IBankAccService {
    /**
     * 新增或更新银行卡信息
     *
     * @param dto
     */
    @Override
    public void add(BankAccountUpsertReqDTO dto) {
        //判断参数
        if (ObjectUtil.isEmpty(dto)){
            throw new BadRequestException("参数错误");
        }
        BankAcc bankAcc = BeanUtils.copyBean(dto, BankAcc.class);
        bankAcc.setId(UserContext.currentUserId());
        boolean b = this.saveOrUpdate(bankAcc);
        if (!b){
            throw new BadRequestException("添加银行卡信息失败");
        }

    }

    /**
     * 获取当前用户的银行卡信息
     *
     * @return
     */
    @Override
    public BankAccountResDTO currentUserBankAccount() {
        BankAcc bankAcc = this.getById(UserContext.currentUserId());
        if (ObjectUtil.isEmpty(bankAcc)){
            throw new BadRequestException("当前用户未添加银行卡信息");
        }
        return BeanUtils.toBean(bankAcc, BankAccountResDTO.class);
    }
}
